﻿using System;

using Storm.TextEditor.Win32;
using Storm.TextEditor.Win32.Structs;

namespace Storm.TextEditor.Drawing.GDI
{
	public class GDIFont
		: GDIObject
	{
		#region Fields

		public bool bold          = false;
		public bool italic        = false;
		public bool underline     = false;
		public bool strikethrough = false;

		public float  size     = 0f;
		public string fontname = "";
		public byte   charset  = 0;
		public IntPtr hFont    = IntPtr.Zero;

		#endregion

		#region Methods

		#region Protected

		protected override void Destroy()
		{
			if (hFont != (IntPtr)0)
				NativeGdi32Api.DeleteObject(hFont);

			base.Destroy();
			hFont = (IntPtr)0;
		}

		#endregion

		#endregion

		/// <summary>
		/// Initializes a new instance of GDIFont.
		/// </summary>
		public GDIFont()
		{
			this.Create();
		}

		/// <summary>
		/// Initializes a new instance of GDIFont.
		/// </summary>
		public GDIFont(string fontname, float size)
			: this(fontname, size, false, false, false, false)
		{
		}

		/// <summary>
		/// Initializes a new instance of GDIFont.
		/// </summary>
		public GDIFont(string fontname, float size, bool bold, bool italic, bool underline, bool strikethrough)
		{
			this.fontname = fontname;
			this.size = size;

			this.bold          = bold;
			this.italic        = italic;
			this.underline     = underline;
			this.strikethrough = strikethrough;

			LogFont tFont = new LogFont();
			tFont.lfItalic = (byte)(italic ? 1 : 0);
			tFont.lfStrikeOut = (byte)(strikethrough ? 1 : 0);
			tFont.lfUnderline = (byte)(underline ? 1 : 0);
			tFont.lfWeight = bold ? 700 : 400;
			tFont.lfWidth = 0;
			tFont.lfHeight = (int)(-size * 1.3333333333333);
			tFont.lfCharSet = 1;

			tFont.lfFaceName = fontname;
			hFont = NativeGdi32Api.CreateFontIndirect(tFont);

			this.Create();
		}

		~GDIFont()
		{
			this.Destroy();
		}
	}
}
